package com.tea.room.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tea.room.service.wechat.WeChatService;

/**
 * 微信消息接口
 * @ClassName:MessageController   
 * @Description:TODO()   
 * @date:2019年4月9日 上午10:43:32 
 * @author ShiP
 */
@RestController("messageController")
public class MessageController {

    @Autowired
    private WeChatService weChatService;
    
    /**
     * token认证接口  get
     * @Title: verifyToken   
     * @Description: TODO()   
     * @param: @param request
     * @param: @param response      
     * @return: void      
     * @throws
     */
    @ResponseBody
    @RequestMapping(value="/verify_wx_token", method=RequestMethod.GET)
    public void verifyToken(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        String echostr = request.getParameter("echostr");
        Boolean token = weChatService.verifyToken(request);
        if(token){
            try {
                out = response.getWriter();
                out.write(echostr);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                out.close();
            }
        }
    }
    
    /**
     * 消息回复接口  post
     * @Title: verifyToken1   
     * @Description: TODO()   
     * @param: @param request
     * @param: @param response      
     * @return: void      
     * @throws
     */
    @ResponseBody
    @RequestMapping(value="/verify_wx_token", method=RequestMethod.POST)
    public void verifyToken1(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            out = response.getWriter();
            String msg = weChatService.messageHandle(request);
            out.write(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }


    
    
    
    
    
    
    
}
